<?php defined('SYSPATH') or die('No direct script access.');

class Model_Autoreply extends ORM
{  
    protected $_table_name = 'autoreplies';
    protected $_reload_on_wakeup = false;
    
    public function save_autoreply(Model_Mailbox $mailbox, $subject, $text)
    {
        $company = Model_Main::get_company();
        if ($company->id != $mailbox->company_id) {
            throw new HTTP_Exception_404('Такой компании нет');    
        }
        
        $subject   = Security::xss_clean($subject);
        $text      = Security::xss_clean($text);
        
        $autoreply = ORM::factory('autoreply', $mailbox->id);
        if (!$autoreply->loaded()) {
            $autoreply = $this;
            $autoreply->id = $mailbox->id;   
        }
        
        $autoreply->subject = $subject;
        $autoreply->text    = $text;       
        $autoreply->save();
        
        return $autoreply;   
    }
}
